#pragma once
#include "ClientSocketStream.h"
#include "Msg.h"
#include "Timer.h"
#include "Socket.h"

class CNetClient;
class CClientSocket
{
public:
	CClientSocket(void);
	~CClientSocket(void);

	virtual BOOL Connect(const char *szServerAddr,int nServerPort);
	static DWORD WINAPI ConnectThread(LPVOID pParam);
	BOOL IsConnected() { return m_bConnected;};
	Socket GetSocket() { return m_Socket; };
	void Close();

	int Fill();
	int Flush();

	void Tick(VOID);
	void TickConnecting(VOID);

	BOOL				Select(VOID);
	BOOL				ProcessExcept(VOID);
	BOOL				ProcessInput(VOID);
	BOOL				ProcessOutput(VOID);
	VOID				ProcessCommands(VOID);

	BOOL SendMsg(Msg *pMsg);

	void SetNetClient(CNetClient *pNetClient) {m_pNetClient = pNetClient;}
	const char *GetServerAddr() {return m_szServerAddr;};
	int GetServerPort() {return m_nServerPort;}
public:
	sockaddr_in m_selfAddr;
protected:

	HANDLE m_hConnectThread;
	char m_szServerAddr[20];
	int	m_nServerPort;
	BOOL m_bConnected;
	BOOL m_bBreak;
	CTimer m_TimerConnect;

	fd_set					m_ReadFD;
	fd_set					m_WriteFD;
	fd_set					m_ExceptFD;
	Socket					m_Socket;
	CClientSocketStream		m_SocketInputStream;
	CClientSocketStream		m_SocketOutputStream;
	CNetClient *m_pNetClient;
};

